<?php
/**
 * Interface zum Db_Connector
 *
 * @author		Joachim Ruf
 * @copyright	Copyright (c) 2003-2010 Loresoft Software (http://www.loresoft.de)
 * @license		http://yapary.loresoft.de/lizenzbedingungen     New BSD License
 *
 * */


class Yapary_Db_Sql_Table Implements Yapary_Db_ITable
{
    private $tableName    = NULL;
    private $connection   = NULL;


    function __construct ($_connection, $_tableName) {
        $this->connection = $_connection;
        $this->tableName  = $_tableName;
    }


    /**
     * Prueft, ob die angegebene Tabelle existiert
     * @return bool
     * */
    public function exists () {
		$arr = array();

		$resId = @mysql_query('SHOW TABLE STATUS WHERE Name="'.$this->tableName.'"', $this->connection);
		if (gettype($resId) !== 'resource') {
		    return false;
		    //trigger_error('FATAL ERROR in <b>'.__METHOD__.'</b> TMP data does not exist', E_USER_ERROR);
		}

		while ($result = @mysql_fetch_array($resId)) {
    		if ($result["Name"] == $this->tableName) {
    			return true;
    		}
		}

		return false;
    }


    /**
     * Gibt eine Instanz der TableRow-Klasse aus
     * @return Yapary_Db_ITableRow
     * */
    public function row () {
        return new Yapary_Db_Sql_TableRow($this->connection, $this->tableName);
    }


    /**
     * Gibt eine Instanz der TableCol-Klasse aus
     * @return Yapary_Db_ITableCol
     * */
    public function col () {
        return new Yapary_Db_Sql_TableCol($this->connection, $this->tableName);
    }


    /**
     * Liefert die Anzahl an verbrauchten Bytes
     * @return int
     * */
    public function getMemoryUsage () {
        $result = 0;
		$resId = @mysql_query('SHOW TABLE STATUS WHERE Name="'.$this->tableName .'"', $this->connection);

		if (gettype($resId) !== 'resource') {
		    return NULL;
		}

	    while ($row = @mysql_fetch_array($resId)) {
            $result += $row['Data_length'] + $row['Index_length'];
	    }

	    return $result;
    }


}


?>